import { escape2Html } from '@/libs/utils'
const common = {
  props: {
    index: {
      //
      type: Number,
      default: 0
    },
    showChecked: {
      //显示选择框
      type: Boolean,
      default: false
    },
    showRightAnswer: {
      //显示正确答案
      type: Boolean,
      default: true
    },
    showExplanation: {
      //显示解析
      type: Boolean,
      default: true
    },
    showAnswerResult: {
      //显示答题结果：正确/错误
      type: Boolean,
      default: false
    },
    showUserAnswer: {
      //显示用户回答
      type: Boolean,
      default: false
    },
    showScore: {
      //显示分数
      type: Boolean,
      default: true
    },
    pattern: {
      //模式 ：答题/显示
      type: String,
      default: 'show',
      validator(value) {
        return ['show', 'answer'].includes(value)
      }
    },
    disabledAnswer: {
      type: Boolean,
      default: true
    },
    disabled: {
      type: [Boolean, Function],
      defaule: false
    },
    directionData: {
      // 类型字典项
      type: Array,
      defaule: () => []
    },
    showNoAnswer: {
      //是否显示此题未作答
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      escape2Html,
      errorImg: require('../images/error.png'),
      correctImg: require('../images/correct.png'),
      desModal: {
        show: false,
        description: ''
      }
    }
  },
  computed: {},
  filters: {
    difficultyFilter: (val) => {
      let difficultyStr = ''
      if (val) {
        val = val.toLowerCase()
      }
      switch (val) {
        case 'simple':
          difficultyStr = '简单'
          break
        case 'commonly':
          difficultyStr = '一般'
          break
        case 'medium':
          difficultyStr = '中等'
          break
        case 'hard':
          difficultyStr = '较难'
          break
      }
      return difficultyStr
    },
    modeFilter: (val) => {
      let str = ''
      if (val) {
        val = parseInt(val)
      }
      switch (val) {
        case 1:
          str = '单选'
          break
        case 2:
          str = '多选'
          break
        case 3:
          str = '判断'
          break
        case 4:
          str = '填空'
          break
        case 5:
          str = '简答'
          break
      }
      return str
    },
    filterIsPublic(type) {
      //1--共享  2--未共享
      let status = ''
      if (type === 'share') {
        status = '单位'
      } else if (type === 'private') {
        status = '个人'
      } else if (type === 'public') {
        status = '系统'
      }
      return status
    }
  },
  created() {},
  methods: {
    answerRight() {
      let answerResult = this.question.answerResult || this.question.answerRight
      return (
        answerResult === 1 ||
        answerResult === '1' ||
        answerResult === true ||
        answerResult === 'right' ||
        answerResult === 'correct'
      )
    },
    handleSelect($event, question) {
      this.$emit('handleSelect', $event, question)
    },
    filterAllPoints(val) {
      if (!val) {
        return []
      }
      //获取所有方向
      let arr = []
      let splitVal = val.split(',')
      let typeCodeObj = this.directionData || []
      splitVal.forEach((split) => {
        let curCode = typeCodeObj.find((code) => code.dictid === split)
        if (curCode) {
          arr.push(curCode.dictname)
        }
      })
      if (arr.length === 0) {
        arr = ['未知类型']
      }
      return arr
    },
    filterTypeCode(val) {
      return this.$parent.handleCodeTypeAndDirection(val)
    },
    showDescModal(description, modeCode) {
      this.desModal.show = true
      this.desModal.modeCode = modeCode
      this.desModal.description = description
    },
    //对题目答案进行排序
    sortQuestionAnswer(answer) {
      if (!answer) {
        return
      } else if (answer.length > 0 && answer[0].no) {
        answer.sort(function(a, b) {
          return parseInt(a.no) - parseInt(b.no)
        })
      }
      return answer
    },
    getAnswerDes({ description = '' }, index) {
      let reg = /^\[[A-Z]\]/
      let regOld = /^\【[A-Z]\】/
      if (regOld.test(description)) {
        description = description.replace(/【([A-Za-z])】/, '[$1]')
      }
      if (!reg.test(description)) {
        return (description = `[${this.filterIndex(index + 1)}] ${description}`)
      } else {
        if (description.substring(3, 4) !== ' ') {
          return description.replace(/\]/g, `] `)
        }
        return description
      }
    },
    filterIndex(val) {
      let str = ''
      switch (val) {
        case 1:
          str = 'A'
          break
        case 2:
          str = 'B'
          break
        case 3:
          str = 'C'
          break
        case 4:
          str = 'D'
          break
        case 5:
          str = 'E'
          break
        case 6:
          str = 'F'
          break
        case 7:
          str = 'G'
          break
        case 8:
          str = 'H'
          break
        case 9:
          str = 'I'
          break
        case 10:
          str = 'J'
          break
        case 11:
          str = 'K'
          break
        case 12:
          str = 'L'
          break
        default:
          str = ''
          break
      }
      return str
    },
    getDisabled(ques) {
      if (this.disabled) {
        return typeof this.disabled === 'function' ? this.disabled(ques) : this.disabled
      } else {
        return ques._disabled
      }
    }
  }
}
export default common
